package com.apes.fn.server.workDesk.repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.scm.report.util.SalesReportUtils;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2023-06-08 10:22
 **/
@Component
public class WorkDeskDao extends ApesDao {

    /**
     * 工作台-门店业绩
     * -入参:DEPT_ID
     * @return
     */
    public JSONArray deptSalesReportDetail(JSONObject param) {
        StringBuffer sf = new StringBuffer();
        sf.append("select SUM(B.SALE_MONEY)SALE_MONEY,SUM(a.BUY_YEARCARD)YEARCARD,SUM(B.COST)COST,SUM(B.TIMES)TIMES,SUM(B.PROFIT)PROFIT\n" +
                "from RCL_DAILYSALESREPORT A left join RCL_SALES_REPORT_DAY b on A.DEPT_ID = b.DEPT_ID and a.DATE_OF_STATISTICS = b.DATE_OF_STATISTICS\n" +
                "where a.DATE_OF_STATISTICS = trunc(sysdate) ");
        SalesReportUtils.appendCondition(sf,param,"A.","DEPT_ID");
        return toJSONArray(find(sf.toString(),param));
    }

    /**
     * 工作台-个人业绩
     * -入参:DEPT_ID
     * @return
     */
    public JSONArray personSalesRpeortDetail(JSONObject param) {
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT * FROM (SELECT A.COMPANY_ID,\n" +
                "       COMPANY_NAME,\n" +
                "       PARTY_ROLE_ID,\n" +
                "       PARTY_ROLE_NAME,\n" +
                "       DEPT_ID,\n" +
                "       DEPT_NAME,\n" +
                "       TJRQ_CHAR,\n" +
                "       TJRQ,\n" +
                "       YYCZ,\n" +
                "       YYCZ+SGCZ CZ,\n" +
                "       YYCZTC,\n" +
                "       YYCZTC+SGTC TC,\n" +
                "       JCTSZY,\n" +
                "       JCTSDG,\n" +
                "       JSTS,\n" +
                "       DCCZ,\n" +
                "       SGCZ,\n" +
                "       WASH_AMOUNT,\n" +
                "       SPRAY_PAINT,\n" +
                "       SGTC,\n" +
                "       (case B.SEX when '000150' then '男' else '女' end)SEX,\n" +
                "       RANK() over (ORDER BY YYCZ+SGCZ DESC ) CZPM\n" +
                "from V_YYCZTJR A LEFT JOIN hr_Employee b on A.PARTY_ROLE_ID = B.ORIGIN\n" +
                "where  A.TJRQ = TRUNC(SYSDATE)  ");
        SalesReportUtils.appendCondition(sf,param,"A.",true,"DEPT_ID");
        sf.append(") a where 1= 1");
        SalesReportUtils.appendCondition(sf,param,"A.",true,"PARTY_ROLE_ID");
        return toJSONArray(find(sf.toString(),param));
    }

}
